import os
import requests


class JenkinsProcess:
    def __init__(self):
        self.root_url = "http://101.245.100.32:8080"

class JenkinsJobProcess(JenkinsProcess):
    def __init__(self, job_name, job_number):
        super(JenkinsJobProcess, self).__init__()
        self.job_name = job_name
        self.job_number = job_number

    def save_log(self, save_path):
        url = f"{self.root_url}/job/{self.job_name}/{self.job_number}/consoleText"
        response = requests.get(url)
        if response.status_code != 200:
            print("Jenkins log获取失败，检查job_name和job_number!")
            return False
        data = response.text
        if os.path.isdir(save_path):
            os.makedirs(save_path, exist_ok=True)
            file_name = f"{self.job_name}_{self.job_number}.log"
            save_path = os.path.join(save_path, file_name)
        else:
            folder_path = os.path.split(save_path)[0]
            os.makedirs(folder_path, exist_ok=True)
        with open(save_path, "w", encoding="utf-8") as log_file:
            log_file.write(data)
        return True
